package com.app.service.impl;

import com.app.mapper.ExposureMapper;
import com.app.model.DepartmentModel;
import com.app.model.ExposureModel;
import com.app.model.SystemModel;
import com.app.service.ExposureService;
import com.app.vo.ExposureVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ExposureServiceImpl extends ServiceImpl<ExposureMapper, ExposureModel> implements ExposureService {
    @Autowired
    private ExposureMapper exposureMapper;

    @Override
    public ExposureVo ExposureList(ExposureModel model) {
        List<ExposureModel> listByModel = getListByModel(model);
        List<SystemModel> systemModels = exposureMapper.selectType();
        for (int i = 0; i < listByModel.size(); i++) {
            for (int i1 = 0; i1 < systemModels.size(); i1++) {
                if (listByModel.get(i).getType() != null && listByModel.get(i).getType() == systemModels.get(i1).getId()) {
                    listByModel.get(i).setTypeText(systemModels.get(i1).getName());
                }
            }
        }
        ExposureVo vo = new ExposureVo();
        if (model.getType() != null && model.getType() == 76) {
            List<ExposureModel> list1 = new ArrayList<>();
            for (int i = 0, j = 0; i < listByModel.size(); i++) {
                if (listByModel.get(i).getType() != null && listByModel.get(i).getType() == 76) {
                    list1.add(j, listByModel.get(i));
                    j++;
                }
            }
            Integer countByType = exposureMapper.getCountByType(model.getType());
            vo.setList(list1);
            vo.setCount(countByType);
            return vo;
        } else if (model.getType() != null && model.getType() == 77) {
            List<ExposureModel> list1 = new ArrayList<>();
            for (int i = 0, j = 0; i < listByModel.size(); i++) {
                if (listByModel.get(i).getType() != null && listByModel.get(i).getType() == 77) {
                    list1.add(j, listByModel.get(i));
                    j++;
                }
            }
            Integer countByType = exposureMapper.getCountByType(model.getType());
            vo.setList(list1);
            vo.setCount(countByType);
            return vo;
        } else if (model.getType() != null && model.getType() == 78) {
            List<ExposureModel> list1 = new ArrayList<>();
            for (int i = 0, j = 0; i < listByModel.size(); i++) {
                if (listByModel.get(i).getType() != null && listByModel.get(i).getType() == 78) {
                    list1.add(j, listByModel.get(i));
                    j++;
                }
            }
            Integer countByType = exposureMapper.getCountByType(model.getType());
            vo.setList(list1);
            vo.setCount(countByType);
            return vo;
        } else if (model.getType() != null && model.getType() == 84) {
            List<ExposureModel> list1 = new ArrayList<>();
            for (int i = 0, j = 0; i < listByModel.size(); i++) {
                if (listByModel.get(i).getType() != null && listByModel.get(i).getType() == 84) {
                    list1.add(j, listByModel.get(i));
                    j++;
                }
            }
            Integer countByType = exposureMapper.getCountByType(model.getType());
            vo.setList(list1);
            vo.setCount(countByType);
            return vo;
        } else {
            for (int i = 0, j = 0; i < listByModel.size(); i++) {
                if (listByModel.get(i).getType() != null && listByModel.get(i).getType() == 84) {
                    listByModel.remove(i);
                }
            }
            for (int j = 0; j < listByModel.size(); j++) {
                if (listByModel.get(j).getType() != null && listByModel.get(j).getType() == 84) {
                    listByModel.remove(j);
                    j++;
                }
            }
            int count = getCountByModel(model);
            vo.setList(listByModel);
            vo.setCount(count);
            return vo;
        }
    }

    @Override
    public List<ExposureModel> getListByModel(ExposureModel model) {
        return exposureMapper.getListByModel(model);
    }

    @Override
    public int getCountByModel(ExposureModel model) {
        return exposureMapper.getCountByModel(model);
    }

    @Override
    public ExposureVo List(ExposureModel model) {
        List<ExposureModel> list = getList(model);
        List<SystemModel> systemModels = exposureMapper.selectType();
        for (int i = 0; i < list.size(); i++) {
            for (int i1 = 0; i1 < systemModels.size(); i1++) {
                if (list.get(i).getType() != null && list.get(i).getType() == systemModels.get(i1).getId()) {
                    list.get(i).setTypeText(systemModels.get(i1).getName());
                }
            }
        }
        ExposureVo vo = new ExposureVo();
        if (model.getType() != null && model.getType() == 76) {
            List<ExposureModel> list1 = new ArrayList<>();
            for (int i = 0, j = 0; i < list.size(); i++) {
                if (list.get(i).getType() != null && list.get(i).getType() == 76) {
                    list1.add(j, list.get(i));
                    j++;
                }
            }
            Integer countByType = exposureMapper.getCountByType(model.getType());
            vo.setList(list1);
            vo.setCount(countByType);
            return vo;
        } else if (model.getType() != null && model.getType() == 77) {
            List<ExposureModel> list1 = new ArrayList<>();
            for (int i = 0, j = 0; i < list.size(); i++) {
                if (list.get(i).getType() != null && list.get(i).getType() == 77) {
                    list1.add(j, list.get(i));
                    j++;
                }
            }
            Integer countByType = exposureMapper.getCountByType(model.getType());
            vo.setList(list1);
            vo.setCount(countByType);
            return vo;
        } else if (model.getType() != null && model.getType() == 78) {
            List<ExposureModel> list1 = new ArrayList<>();
            for (int i = 0, j = 0; i < list.size(); i++) {
                if (list.get(i).getType() != null && list.get(i).getType() == 78) {
                    list1.add(j, list.get(i));
                    j++;
                }
            }
            Integer countByType = exposureMapper.getCountByType(model.getType());
            vo.setList(list1);
            vo.setCount(countByType);
            return vo;
        } else if (model.getType() != null && model.getType() == 84) {
            List<ExposureModel> list1 = new ArrayList<>();
            for (int i = 0, j = 0; i < list.size(); i++) {
                if (list.get(i).getType() != null && list.get(i).getType() == 84) {
                    list1.add(j, list.get(i));
                    j++;
                }
            }
            Integer countByType = exposureMapper.getCountByType(model.getType());
            vo.setList(list1);
            vo.setCount(countByType);
            return vo;
        } else {
            for (int i = 0, j = 0; i < list.size(); i++) {
                if (list.get(i).getType() != null && list.get(i).getType() == 84) {
                    list.remove(i);
                }
            }
            int count = getCount(model);
            vo.setList(list);
            vo.setCount(count);
            return vo;
        }
    }

    @Override
    public List<ExposureModel> getList(ExposureModel model) {
        return exposureMapper.getList(model);
    }

    @Override
    public int getCount(ExposureModel model) {
        return exposureMapper.getCount(model);
    }

    @Override
    public ExposureModel getContentById(int id) {
        return exposureMapper.getContentById(id);
    }


    @Override
    public ExposureModel add(String title, String content, int sort, int isHeat, Integer type) {
        ExposureModel exposureModel = new ExposureModel();
        exposureModel.setExposureTitle(title);
        exposureModel.setExposureContent(content);
        exposureModel.setIsHeat(isHeat);
        exposureModel.setSort(sort);
        exposureModel.setType(type);
        exposureModel.setCreateTime(LocalDateTime.now());
        exposureMapper.insert(exposureModel);
        return exposureModel;
    }

    @Override
    public ExposureModel update(String title, String content, int sort, int isHeat, int id, Integer type) {
        ExposureModel exposureModel = exposureMapper.selectById(id);
        exposureModel.setExposureTitle(title);
        exposureModel.setExposureContent(content);
        exposureModel.setSort(sort);
        exposureModel.setIsHeat(isHeat);
        exposureModel.setType(type);
        exposureModel.setUpdateTime(LocalDateTime.now());
        exposureMapper.updateById(exposureModel);
        return exposureModel;
    }

    @Override
    public void deleteExposureById(int id) {
        exposureMapper.deleteById(id);
    }

    @Override
    public ExposureModel selectById(int id, int isHeat) {
        ExposureModel exposureModel = exposureMapper.selectById(id);
        exposureModel.setIsHeat(isHeat);
        exposureModel.setUpdateTime(LocalDateTime.now());
        exposureMapper.updateById(exposureModel);
        return exposureModel;
    }

    @Override
    public List<SystemModel> selectType() {
        return exposureMapper.selectType();
    }
}
